{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bqplot import Label, LinearScale, Axis, Lines, Figure, DateScale\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "np.random.seed(10)\n",
    "price_data = pd.DataFrame(np.cumsum(np.random.randn(150, 2).dot([[0.5, 0.8], [0.8, 1.0]]), axis=0) + 100,\n",
    "                          columns=['Security 1', 'Security 2'],\n",
    "                          index=pd.date_range(start='01-01-2007', periods=150))\n",
    "y_data = np.cumsum(np.random.randn(100))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Label positioned in data co-ordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_sc = LinearScale()\n",
    "y_sc = LinearScale()\n",
    "\n",
    "test_line = Lines(x=np.arange(10), y=y_data[:10], scales={'x': x_sc, 'y': y_sc})\n",
    "test_label = Label(x=np.arange(5), y=y_data[:5], scales={'x': x_sc, 'y': y_sc},\n",
    "                   text=['Test', 'Label', 'for', 'the', 'Data'], default_size=26, font_weight='bolder',\n",
    "                   colors=['orange', 'red'], update_on_move=True)\n",
    "\n",
    "ax_x = Axis(scale=x_sc, label='X')\n",
    "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f', label='Y')\n",
    "\n",
    "Figure(marks=[test_label, test_line], axes=[ax_x, ax_y], title='Basic Label Example')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Setting the label attribute `enable_move` to `True` makes the label draggable"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_label.enable_move = True"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Label positioned in terms of Figure co-ordinates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x_sc = LinearScale()\n",
    "y_sc = LinearScale()\n",
    "\n",
    "test_line = Lines(x=np.arange(10), y=y_data,  scales={'x': x_sc, 'y': y_sc})\n",
    "test_label = Label(x=[0.5], y=[0.2], text=['Test Label'], default_size=26,\n",
    "                  font_weight='bolder', colors=['orange'])\n",
    "\n",
    "ax_x = Axis(scale=x_sc)\n",
    "ax_y = Axis(scale=y_sc, orientation='vertical', tick_format='0.2f')\n",
    "\n",
    "Figure(marks=[test_line, test_label], axes=[ax_x, ax_y])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Rotating the label\n",
    "test_label.rotate_angle = 30"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Label positioned at a Date value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dt_sc = DateScale()\n",
    "y_sc = LinearScale()\n",
    "\n",
    "lines = Lines(x=price_data.index.values, y=price_data['Security 1'].values, scales={'x': dt_sc, 'y': y_sc})\n",
    "label = Label(x=[np.datetime64('2007-03-14')], y=[0.5], scales={'x': dt_sc}, text=['Pi-Day'], colors=['orange'])\n",
    "\n",
    "ax_x = Axis(scale=dt_sc)\n",
    "ax_y = Axis(scale=y_sc, orientation='vertical')\n",
    "\n",
    "Figure(marks=[lines, label], axes=[ax_x, ax_y])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Setting an offset in pixel\n",
    "label.x_offset = 100"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
